package org.meetunexpectedly.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.meetunexpectedly.entity.response.AuditResponse;
import org.meetunexpectedly.entity.user.User;
import org.meetunexpectedly.entity.video.CommentThumb;
import org.meetunexpectedly.entity.video.Video;
import org.meetunexpectedly.entity.video.VideoComment;
import org.meetunexpectedly.entity.vo.BasePage;
import org.meetunexpectedly.exception.BaseException;
import org.meetunexpectedly.holder.UserHolder;
import org.meetunexpectedly.mapper.video.VideoCommentMapper;
import org.meetunexpectedly.service.audit.TextAuditService;
import org.meetunexpectedly.service.user.UserService;
import org.meetunexpectedly.service.video.VideoCommentService;
import org.meetunexpectedly.service.video.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * @Description: 视频评论服务实现类
 * @Author: pupil
 * @Date: 2025/1/23 下午 4:59
 */
@Service
public class VideoCommentServiceImpl extends ServiceImpl<VideoCommentMapper, VideoComment> implements VideoCommentService {
    @Autowired
    private VideoService videoService;

    @Autowired
    private TextAuditService textAuditService;

    @Autowired
    private UserService userService;

    @Override
    public Boolean addComment(Long videoId, String content, Long userId) {
        if (content == null) {
            throw new BaseException("评论内容不能为空");
        }

        // 判断视频是否存在
        Video video = videoService.getById(videoId);
        if (video == null) {
            throw new BaseException("指定视频不存在");
        }

        // 进行内容文本审核
        AuditResponse audit = textAuditService.audit(content);
        if (!audit.getMsg().equals("正常")) {
            throw new BaseException("评论内容违规");
        }

        VideoComment comment = new VideoComment();
        // 设置评论用户id
        comment.setUserId(userId);
        // 设置评论内容
        comment.setContent(content);
        // 设置视频id
        comment.setVideoId(videoId);
        // 设置视频作者id
        comment.setVideoUserId(video.getUserId());

        // 存储评论
        this.saveOrUpdate(comment);

        return null;
    }

    @Override
    public Collection<VideoComment> getCommentByVideoId(BasePage basePage, Long videoId) {
        // 判断分页信息是否为空
        if (ObjectUtils.isEmpty(basePage)) {
            throw new BaseException("分页信息不能为空");
        }

        // 判断视频是否存在
        Video video = videoService.getById(videoId);
        if (video == null) {
            throw new BaseException("指定视频不存在");
        }

        LambdaQueryWrapper<VideoComment> wrapper = new LambdaQueryWrapper<>();
        // 分页查询
        IPage<VideoComment> page = this.page(basePage.page(),
                wrapper.eq(VideoComment::getVideoId, videoId)
                        .orderByDesc(VideoComment::getGmtUpdated));
        Long userId = UserHolder.get();
        Collection<VideoComment> commentList = setUser(page, userId);

        return  commentList;
    }

    @Override
    public Boolean deleteCommentById(Long commentId, Long userId) {
        if (userId == null) {
            throw new BaseException("userId不能为空");
        }
        VideoComment comment = this.getById(commentId);
        if (ObjectUtils.isEmpty(comment)) {
            throw new BaseException("指定评论不存在");
        }

        // 删除评论
        boolean result = this.remove(new LambdaQueryWrapper<VideoComment>().eq(VideoComment::getId, commentId));
        return result;
    }

    /**
     * 设置user
     *
     * @param page        分页查询结果
     * @param videoUserId 视频作者用户id
     * @return
     */
    private Collection<VideoComment> setUser(IPage<VideoComment> page, Long videoUserId) {
        Collection<VideoComment> commentList = page.getRecords();
        if (ObjectUtils.isEmpty(commentList)) {
            return Collections.EMPTY_LIST;
        }

        commentList.stream().filter(comment -> comment != null).collect(Collectors.toList());

        for (VideoComment comment : commentList) {
            Long userId = comment.getUserId();
            // 判断是否为视频作者的评论
            if (userId == videoUserId) {
                comment.setIsVideoUser(true);
            }
            // 设置评论用户信息
            User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, userId).select(User::getId, User::getNickName, User::getAvatar));
            comment.setUser(user);
        }
        return commentList;
    }
}
